﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;


namespace Lawyer.Web.Admin
{
    public partial class admin_activitytype : System.Web.UI.Page
    {
        Lawyer.BLL.ActivityTypeInfo typeBll = new Lawyer.BLL.ActivityTypeInfo();
        Lawyer.BLL.ActivityInfo activityBll = new Lawyer.BLL.ActivityInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            ActivityTypeDataBind();

        }

        public void ActivityTypeDataBind()
        {
            string strWhere = "";
            List<Lawyer.Model.ActivityTypeInfo> list = typeBll.GetModelList(strWhere);
            ActivityTypeList.DataSource = list;
            ActivityTypeList.DataBind();
            if (ActivityTypeList.Rows.Count > 0)
            {
                ActivityTypeList.HeaderRow.TableSection = TableRowSection.TableHeader;
            }
        }

        protected void ActivityTypeList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            LinkButton btnDelete = (LinkButton)e.Row.FindControl("btnDelete");
            LinkButton btnCount = (LinkButton)e.Row.FindControl("btnCount");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string typeName = e.Row.Cells[1].Text.ToString();

                string strWhere = "Type = '" + typeName + "'";
                List<Model.ActivityInfo> list = activityBll.GetModelList(strWhere);
                if (list.Count == 0 || list == null)
                {
                    btnCount.Text = "0";
                }
                else
                {
                    btnCount.Text = list.Count.ToString();
                }
                
                btnDelete.CommandName = "DeleteType";
                btnCount.CommandName = "TypeCount";
            }
        }

        protected void ActivityTypeList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string typeID = "";
            
            if (e.CommandName == "DeleteType")
            {
                typeID = e.CommandArgument.ToString();
                Model.ActivityTypeInfo model = typeBll.GetModel(typeID);
                string strWhere = " Type = '" + model.TypeName + "'";
                List<Lawyer.Model.ActivityInfo> list = activityBll.GetModelList(strWhere);
                if (list.Count == 0 || list == null)
                {
                    if (typeBll.Delete(typeID))
                    {
                        Maticsoft.Common.MessageBox.ShowAndRedirect(this, "Delete successfully!", "admin_activitytype.aspx");
                        return;
                    }
                    else
                    {
                        Maticsoft.Common.MessageBox.ShowAndRedirect(this, "Delete failed!", "admin_activitytype.aspx");
                    }
                }
                else
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "There are some activity related with the type", "admin_activitytype.aspx");
                }
            }
            if (e.CommandName == "TypeCount")
            {
                typeID = e.CommandArgument.ToString();
                Model.ActivityTypeInfo model = typeBll.GetModel(typeID);
                string strWhere = " Type = '" + model.TypeName + "'";
                List<Lawyer.Model.ActivityInfo> list = activityBll.GetModelList(strWhere);
                if (list.Count == 0 || list == null)
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "There are no activity related with the type", "admin_activitytype.aspx");
                }
                else
                {
                    Response.Redirect("admin_activity.aspx?type=" + model.TypeName);
                }
            }
        }
    }
}
